package com.mysen.sso.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.mysen.exception.MaikeServiceException;
import com.mysen.model.BaseModel;
import com.mysen.model.BaseModel.Status;
import com.mysen.model.Pagenation;
import com.mysen.model.Response;
import com.mysen.sso.dao.PermissionDao;
import com.mysen.sso.dao.RoleDao;
import com.mysen.sso.dao.UserDao;
import com.mysen.sso.model.Permission;
import com.mysen.sso.model.Role;
import com.mysen.sso.model.User;
import com.mysen.sso.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private UserDao dao;

	@Autowired
	private PermissionDao permissionDao;
	
	@Autowired
	private RoleDao roleDao;
	
	public User loginByWeb(User param) {
		LOGGER.info("get User by login start ...");
		param.setStatus(Status.ALLOW);
		List<User> users = dao.selectByPage(param);
		if(users == null || users.size() != 1)
			return null;
		User user = users.get(0);
		
		Role roleParam = new Role();
		roleParam.setUser(user);
		List<Role> roles = roleDao.selectByPage(roleParam);
		user.setRoles(new HashSet<>(roles));
		return user;
	}
	
	public List<Permission> selectPermissions(User user) {
		Permission permissionParam = new Permission();
		permissionParam.setRoles(new HashSet<>(user.getRoles()));
		return permissionDao.selectByPage(permissionParam);
	}

	public boolean existsUserByName(String userName) {
		User user = new User();
		user.setUserName(userName);
		user.setPage(new Pagenation(0, 1));
		return dao.selectByPage(user).size() > 0;
	}
	/**
	 * 登陆或创建后登陆微信用户
	 * */
	public User loginByOpenId(User param) {
		LOGGER.info("get User by login start ...");
		param.setStatus(Status.ALLOW);
		User user = null;
		List<User> users = dao.selectByPage(param);
		if (users == null || users.size() != 1) {
			Set<Role> roles = new HashSet<Role>();
			roles.add(new Role("customer"));
			param.setRoles(roles);
			user = save(param);
		} else {
			user = users.get(0);
		}
		
		if(BaseModel.Status.ALLOW.equals(user.getStatus())) {
			Role roleParam = new Role();
			roleParam.setUser(user);
			List<Role> roles = roleDao.selectByPage(roleParam);
			user.setRoles(new HashSet<>(roles));
			return user;
		}else {
			throw new MaikeServiceException("USER_DISALLOW");
		}
	}

	@Override
	public Response<?> selectByPage(User record) {
		Response<?> resp = new Response<>();
		resp.setTotal(dao.selectByPageCount(record));
		if(resp.getTotal() > 0) {
			resp.setDatas(dao.selectByPage(record));
		}
		return resp;
	}

	@Override
	public int deleteById(String id) {
		return dao.deleteById(id);
	}

	@Override
	public User save(User user) {
		dao.insert(user);
		if(!CollectionUtils.isEmpty(user.getRoles())){
			dao.addRoles(user);
		}
		return user;
	}

	@Override
	public User selectById(String id) {
		return dao.selectById(id);
	}

	@Override
	public int updateById(User record) {
		return dao.updateById(record);
	}

	@Override
	public void addRoles(User user) {
		dao.addRoles(user);
	}

	@Override
	public User selectByOpenId(String openId) {
		User u = new User();
		u.setOpenId(openId);
		List<User> page = dao.selectByPage(u);
		return page != null && page.size() == 1 ? page.get(0) : null;
	}
}
